﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using HYView;

namespace YJKArch.AxisModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdGreateGridByCurve : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Transaction transaction = null;
			Result result;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException("commandData");
				}
				CurveToGridForm curveToGridForm = new CurveToGridForm(cmdData);
				DialogResult dialogResult;
				while ((dialogResult = curveToGridForm.ShowDialog()) == DialogResult.OK && curveToGridForm.ResultSelect)
				{
					curveToGridForm.LayerStyleName();
					string layerStyle = "";
					if (this.SelectLineStyleName(cmdData, ref layerStyle))
					{
						curveToGridForm.SetLayerStyle(layerStyle);
					}
				}
				if (dialogResult == DialogResult.Cancel)
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
					transaction = new Transaction(document);
					transaction.Start("Create Grid By Curve");
					List<string> lineTypeName = curveToGridForm.LayerStyleName();
					IList<Reference> list = null;
					Selection selection = cmdData.Application.ActiveUIDocument.Selection;
					try
					{
						list = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new ModelCurveFilter(lineTypeName), "选择线");
					}
					catch (Exception)
					{
						if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
						{
							transaction.RollBack();
						}
						return Autodesk.Revit.UI.Result.Failed;
					}
					List<Curve> list2 = new List<Curve>();
					foreach (Reference reference in list)
					{
						Element element = document.GetElement(reference);
						Curve geometryCurve;
						if (element.GetType() == typeof(ModelLine))
						{
							geometryCurve = (element as ModelLine).GeometryCurve;
						}
						else if (element.GetType() == typeof(ModelArc))
						{
							geometryCurve = (element as ModelArc).GeometryCurve;
						}
						else if (element.GetType() == typeof(DetailLine))
						{
							geometryCurve = (element as DetailLine).GeometryCurve;
						}
						else
						{
							if (!(element.GetType() == typeof(DetailArc)))
							{
								continue;
							}
							geometryCurve = (element as DetailArc).GeometryCurve;
						}
						if (geometryCurve.IsBound)
						{
							list2.Add(geometryCurve);
						}
					}
					new GridCreatorByCurve(cmdData, curveToGridForm.GetGridType(), curveToGridForm.ExtendLength()).CreateGridByCurves(list2);
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				msg = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private bool SelectLineStyleName(ExternalCommandData cmdData, ref string lineStyleName)
		{
			Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
			Reference reference = null;
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			try
			{
				reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ModelCurveFilter(), "选择模型线");
			}
			catch (Exception)
			{
				return false;
			}
			Element element = document.GetElement(reference);
			if (element.GetType() == typeof(ModelLine))
			{
				ModelLine modelLine = element as ModelLine;
				lineStyleName = modelLine.LineStyle.Name;
			}
			else if (element.GetType() == typeof(ModelArc))
			{
				ModelArc modelArc = element as ModelArc;
				lineStyleName = modelArc.LineStyle.Name;
			}
			else if (element.GetType() == typeof(DetailLine))
			{
				DetailLine detailLine = element as DetailLine;
				lineStyleName = detailLine.LineStyle.Name;
			}
			else if (element.GetType() == typeof(DetailArc))
			{
				DetailArc detailArc = element as DetailArc;
				lineStyleName = detailArc.LineStyle.Name;
			}
			return true;
		}
	}
}
